package com.ruoyi.projects.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.projects.domain.Img;
import com.ruoyi.projects.service.IImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图片信息Controller
 *
 * @author yangz
 * @date 2023-02-02
 */
@Controller
@RequestMapping("/bSystem/img")
public class ImgController extends BaseController
{
    private String prefix = "bSystem/img";

    @Autowired
    private IImgService imgService;

    @GetMapping()
    public String img()
    {
        return prefix + "/img";
    }

    /**
     * 查询图片信息列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Img img)
    {
        startPage();
        List<Img> list = imgService.selectImgList(img);
        return getDataTable(list);
    }

    /**
     * 导出图片信息列表
     */
    @Log(title = "图片信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Img img)
    {
        List<Img> list = imgService.selectImgList(img);
        ExcelUtil<Img> util = new ExcelUtil<Img>(Img.class);
        return util.exportExcel(list, "图片信息数据");
    }

    /**
     * 新增图片信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存图片信息
     */
    @Log(title = "图片信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Img img)
    {
        return toAjax(imgService.insertImg(img));
    }

    /**
     * 修改图片信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Img img = imgService.selectImgById(id);
        mmap.put("img", img);
        return prefix + "/edit";
    }

    /**
     * 修改保存图片信息
     */
    @Log(title = "图片信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Img img)
    {
        return toAjax(imgService.updateImg(img));
    }

    /**
     * 删除图片信息
     */
    @Log(title = "图片信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(imgService.deleteImgByIds(ids));
    }
}
